import store from '@/store'
import { Message } from 'element-ui'

function hasClass(el, className) {
  const reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
  return reg.test(el.className)
}

function checkAdmin(el, binding) {
  const { isAdmin } = store.getters
  if (!isAdmin) {
    el.title = el.title + '\n仅系统管理员可执行此操作'

    // 样式设置，可以改进
    if (hasClass(el, 'el-button--primary')) {
      el.style.background = '#ddd'
      el.style.border = '0px'
    }
    el.style.color = 'gray'

    el.addEventListener('click', (e) => {
      Message.error('仅系统管理员可执行此操作')
      e.stopPropagation()
    }, true)
  }
}

export default {
  inserted(el, binding) {
    checkAdmin(el, binding)
  },
  // update(el, binding) {
  //   checkAdmin(el, binding)
  // }
}
